/*

Replication code for Ben Cormier, "Democracy, Public Debt Transparency, and Sovereign Creditworthiness," forthcoming. 
Governance. 

Note: requires estout package

*/


**************
*Data and set*
**************
use "data1_final_VarsUsedOnly.dta", clear
tsset country_num year


********************************************************************************************
*Moody's Models, Regression Table (Table 2), and Linear Predicted Probability Plot (Fig 1)*
********************************************************************************************
*OLS
regress Moodys_LTOrder PEFAsum_backfill i.year, robust cluster(country_num)
estimates store M1
*
regress Moodys_LTOrder v2x_regime v2x_rule v2xcl_prpty i.year, robust cluster(country_num)
estimates store M2
*
regress Moodys_LTOrder PEFAsum_backfill v2x_regime v2x_rule v2xcl_prpty i.year, robust cluster(country_num)
estimates store M3
*
regress Moodys_LTOrder PEFAsum_backfill v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.year, robust cluster(country_num)
estimates store M4
*Heckman
heckman Mood_if_PEFApartic_backfill PEFAsum_backfill v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year, twostep ///
select(v2psoppaut ODAperGNI anyDummy v2x_regime v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram ka_open)
estimates store M5
*Simple country RE
mixed Moodys_LTOrder PEFAsum_backfill v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year ||country_num: , 
estimates store M6
*Country RE nested in regime type level
mixed Moodys_LTOrder PEFAsum_backfill v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year ||v2x_regime: ||country_num: , covariance(unstructured) vce(cluster v2x_regime)
estimates store M7
*2SLS FEs
ivregress 2sls Moodys_LTOrder v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year i.country_num /// 
(PEFAsum_backfill = PEFA_ReportYr_Dummy), robust cluster(country_num) first
estimates store M8

*Table
esttab M1 M2 M3 M4 M5 M6 M7 M8 using Table1_Moodys_Main.csv, /// 
varlabels("PEFAsum_backfill" "PEFA" "v2x_regime" "Democracy" "v2x_rule" "Law" "v2xcl_prpty" "PropRights" "v2xnp_regcorr" "Corruption" "v2cltrnslw" "GenTpcy" "v2psoppaut" "Opposition" "PPGdebtService" "DebtService" "logPPGPrivDisb" "PrivateCredit" "FinReq" "Deficit" "SandPInvestmentGrade" "S&PInvGrade" "TradeVol_PerGDP" "Trade") ///
collabels(none) mgroups("OLS" "Heckman" "RE" "Nested RE" "2SLS" , pattern (1 0 0 0 1 1 1)) ///
cells(b(star fmt(%9.3f)) se(par)) stats(N) starlevels(* 0.1 ** 0.05 *** 0.01) nonumbers legend /// 
title("Table 2: Models of Public Debt Transparency and Moody's Ratings") /// 
note("Cluster-Robust SEs in OLS, Nested RE, and FE2SLS Estimations" /// 
"Heckman Two-Step SEs in Heckman Estimations" /// 
"Nested RE models allow for unstructured covariance")

*2SLS Linear Predicted Probability Plot
ivregress 2sls Moodys_LTOrder v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year i.country_num /// 
(PEFAsum_backfill = PEFA_ReportYr_Dummy), robust cluster(country_num) first
predict yhatMoodysUnratedCoded
grstyle init
grstyle set plain, nogrid
twoway lfitci yhatMoodysUnratedCoded PEFAsum_backfill, ///
	ciplot(rline) alcolor(black) alpattern(dash) fcolor(none) ///
	ytitle("Moody's Credit Rating") ytitle(, size(small)) ylabel(,labsize(small)) ///
	xtitle("Public Debt Transparency (PDT)") xtitle(, size(small)) xlabel(,labsize(small)) ///
	ylabel(13 "Ba3" 14 "B1" 15 "B2" 16 "B3" 17 "Caa1" 18 "Caa2" 19 "Caa3" 20 "Ca" 21 "C") ysc(reverse) ///
	xlabel(1(1)12)  ///
	legend(off) title("Figure 1: Linear Predictions of Moody's Ratings by Public Debt Transparency") title(, size(small)) ///
	note("2SLS from Table 2, Model 8" "95% Confidence Interval" "Slope inverted from table estimation for ease of interpretation", size(small))
	
	
*****************************************************************************************
*Fitch Models, Regression Table (Table 3), and Linear Predicted Probability Plot (Fig 2)*
*****************************************************************************************
*OLS
regress Fitch_LTOrder PEFAsum_backfill i.year, robust cluster(country_num)
estimates store F1
*
regress Fitch_LTOrder v2x_regime v2x_rule v2xcl_prpty i.year, robust cluster(country_num)
estimates store F2
*
regress Fitch_LTOrder PEFAsum_backfill v2x_regime v2x_rule v2xcl_prpty i.year, robust cluster(country_num)
estimates store F3
*
regress Fitch_LTOrder PEFAsum_backfill v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.year, robust cluster(country_num)
estimates store F4
*Heckman
heckman Fitch_if_PEFApartic_backfill PEFAsum_backfill v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year, twostep ///
select(v2psoppaut ODAperGNI anyDummy v2x_regime v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram ka_open)
estimates store F5
*Simple country RE
mixed Fitch_LTOrder PEFAsum_backfill v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year ||country_num: ,
estimates store F6
*Country RE nested in regime type level
mixed Fitch_LTOrder PEFAsum_backfill v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year ||v2x_regime: ||country_num: , covariance(unstructured) vce(cluster v2x_regime)
estimates store F7
*2SLS, includes country FEs
ivregress 2sls Fitch_LTOrder v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year i.country_num /// 
(PEFAsum_backfill = PEFA_ReportYr_Dummy), robust cluster(country_num) first
estimates store F8

*Table
esttab F1 F2 F3 F4 F5 F6 F7 F8 using Table2_F_Main.csv, /// 
varlabels("PEFAsum_backfill" "PEFA" "v2x_regime" "Democracy" "v2x_rule" "Law" "v2xcl_prpty" "PropRights" "v2xnp_regcorr" "Corruption" "v2cltrnslw" "GenTpcy" "v2psoppaut" "Opposition" "PPGdebtService" "DebtService" "logPPGPrivDisb" "PrivateCredit" "FinReq" "Deficit" "SandPInvestmentGrade" "S&PInvGrade" "TradeVol_PerGDP" "Trade") ///
collabels(none) mgroups("OLS" "Heckman" "RE" "Nested RE" "2SLS(FE)" , pattern (1 0 0 1 1 1 1)) ///
cells(b(star fmt(%9.3f)) se(par)) stats(N) starlevels(* 0.1 ** 0.05 *** 0.01) nonumbers legend /// 
title("Table 3: Models of Public Debt Transparency and Fitch Ratings") /// 
note("Cluster-Robust SEs in OLS, Nested RE, and FE2SLS Estimations" /// 
"Heckman Two-Step SEs in Heckman Estimations" /// 
"Nested RE models allow for unstructured covariance")

*2SLS Linear Predicted Probability Plot
ivregress 2sls Fitch_LTOrder v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year i.country_num /// 
(PEFAsum_backfill = PEFA_ReportYr_Dummy), robust cluster(country_num) first
predict yhatFitch
grstyle init
grstyle set plain, nogrid
twoway lfitci yhatFitch PEFAsum_backfill, level (90) ///
	ciplot(rline) alcolor(black) alpattern(dash) fcolor(none) ///
	ytitle("Fitch Credit Rating") ytitle(, size(small)) ylabel(,labsize(small)) ///
	xtitle("Public Debt Transparency (PDT)") xtitle(, size(small)) xlabel(,labsize(small)) ysc(reverse) ///
	ylabel(14 "B+" 15 "B" 16 "B-" 17 "CCC+" 18 "CCC" 19 "CCC-" 20 "CC" 21 "C") ///
	xlabel(1(1)12)  ///
	legend(off) title("Figure 2: Linear Predictions of Fitch Ratings by Public Debt Transparency") title(, size(small)) ///
	note("2SLS from Table 3, Model 8" "90% Confidence Interval" "Slope inverted from table estimation for ease of interpretation", size(small))


*****************************************************************************************
*S&P Models, Regression Table (Table 4), and Linear Predicted Probability Plot (Fig 3)*
*****************************************************************************************
*******
*S&P Unrated Coded
*******
*OLS
regress SandP_LTOrder PEFAsum_backfill i.year, robust cluster(country_num)
estimates store SP1
*
regress SandP_LTOrder v2x_regime v2x_rule v2xcl_prpty i.year, robust cluster(country_num)
estimates store SP2
*
regress SandP_LTOrder PEFAsum_backfill v2x_regime v2x_rule v2xcl_prpty i.year, robust cluster(country_num)
estimates store SP3
*
regress SandP_LTOrder PEFAsum_backfill v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.year, robust cluster(country_num)
estimates store SP4
*Heckman
heckman SP_if_PEFApartic_backfill PEFAsum_backfill v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year, twostep ///
select(v2psoppaut ODAperGNI anyDummy v2x_regime v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram ka_open)
estimates store SP5
*Simple country RE
mixed SandP_LTOrder PEFAsum_backfill v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year ||country_num: ,
estimates store SP6
*Country RE nested in regime type level
mixed SandP_LTOrder PEFAsum_backfill v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year ||v2x_regime: ||country_num: , covariance(unstructured) vce(cluster v2x_regime)
estimates store SP7
*2SLS, includes country FEs
ivregress 2sls SandP_LTOrder v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year i.country_num /// 
(PEFAsum_backfill = PEFA_ReportYr_Dummy), robust cluster(country_num) first
estimates store SP8

*Table
esttab SP1 SP2 SP3 SP4 SP5 SP6 SP7 SP8 using Table3_SP_Main.csv, /// 
varlabels("PEFAsum_backfill" "PEFA" "v2x_regime" "Democracy" "v2x_rule" "Law" "v2xcl_prpty" "PropRights" "v2xnp_regcorr" "Corruption" "v2cltrnslw" "GenTpcy" "v2psoppaut" "Opposition" "PPGdebtService" "DebtService" "logPPGPrivDisb" "PrivateCredit" "FinReq" "Deficit" "SandPInvestmentGrade" "S&PInvGrade" "TradeVol_PerGDP" "Trade") ///
collabels(none) mgroups("OLS" "Heckman" "RE" "Nested RE" "2SLS" , pattern (1 0 0 0 1 1 1 1)) ///
cells(b(star fmt(%9.3f)) se(par)) stats(N) starlevels(* 0.1 ** 0.05 *** 0.01) nonumbers legend /// 
title("Table 4: Models of Public Debt Transparency and S&P Ratings") /// 
note("Cluster-Robust SEs in OLS, Nested RE, and FE2SLS Estimations" /// 
"Heckman Two-Step SEs in Heckman Estimations" /// 
"Nested RE models allow for unstructured covariance")

*2SLS Linear Predicted Probability Plot
ivregress 2sls SandP_LTOrder v2x_regime v2x_rule v2xcl_prpty v2cltrnslw GDPpcap GDPgrowth Inflation ExtDebtStock FinReq IMFprogram TradeVol_PerGDP i.regionCategory i.incomeCategory i.year i.country_num /// 
(PEFAsum_backfill = PEFA_ReportYr_Dummy), robust cluster(country_num) first
predict yhatSandP
grstyle init
grstyle set plain, nogrid
twoway lfitci yhatSandP PEFAsum_backfill, ///
	ciplot(rline) alcolor(black) alpattern(dash) fcolor(none) ///
	ytitle("S&P Credit Rating") ytitle(, size(small)) ylabel(,labsize(small)) ///
	xtitle("Public Debt Transparency (PDT)") xtitle(, size(small)) xlabel(,labsize(small)) ///
	ylabel(13 "BB-" 14 "B+" 15 "B" 16 "B-" 17 "CCC+" 18 "CCC" 19 "CCC-" 20 "CC" 21 "C") ysc(reverse) ///
	xlabel(1(1)12)  ///
	legend(off) title("Figure 3: Linear Predictions of S&P Ratings by Public Debt Transparency") title(, size(small)) ///
	note("2SLS from Table 4, Model 8" "95% Confidence Interval" "Slope inverted from table estimation for ease of interpretation", size(small))
